package com.fcl.blog.web.user;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.fcl.blog.comment.Result;
import com.fcl.blog.dto.NewRecommend;
import com.fcl.blog.dto.UserBlog;
import com.fcl.blog.po.Admin;
import com.fcl.blog.po.Blog;
import com.fcl.blog.po.TypeTop;
import com.fcl.blog.service.BlogService;
import com.fcl.blog.service.TypeService;
import com.fcl.blog.service.UserServie;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 付
 * @Date 2023/10/29 12:31
 * @Version 1.0
 * @description  用户端的博客首页
 * @since 1.0
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserBlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private UserServie userServie;
    @Autowired
    private TypeService typeService;


    /**
     * 查询所有的博客，除了未发表的文章外
     * @return
     */
    @GetMapping("/blogs")
    public Result userListBlogs(){

        Long total = blogService.blogCount();
        PageHelper.startPage(1,5);
        // 查询所有的博客列表
        List<Blog> listBlog = blogService.selectListBlog();
        List<UserBlog> userBlogs = new ArrayList<>();
        // 查询所有的用户信息
        listBlog.forEach(blog -> {
            UserBlog userBlog = new UserBlog();
            String userId = blog.getUserId();
            Admin admin = userServie.selectById(userId);
            //添加用户的用户名
            userBlog.setUsername(admin.getName());
            // 设置用户头像
            userBlog.setHeadImg(admin.getHeadImg());
            // 博客首图
            userBlog.setFlag(blog.getFlag());
            // 博客分类
            userBlog.setTypeName(typeService.selectById(blog.getTypeId()).getName());
            // 博客描述
            try {
                userBlog.setDescription(blog.getContent().substring(0,40) +"...");
            }catch (Exception e){
                throw new RuntimeException();
            }

            // 设置修改时间
            userBlog.setUpdateTime(DateUtil.format(blog.getUpdateTime(),"yyyy-MM-dd"));
            // 设置博客标题
            userBlog.setTitle(blog.getTitle());
            // 设置观看人数
            userBlog.setViews(RandomUtil.randomLong(1,99999));
            userBlog.setId(blog.getId());

            userBlogs.add(userBlog);
        });


        PageInfo<UserBlog> pageInfo = new PageInfo<>(userBlogs);
        pageInfo.setTotal(total);
        return Result.success(pageInfo);
    }


    @GetMapping("/pageBlog")
    public Result getPage(@RequestParam("page") String page){
        Long total = blogService.blogCount();
        PageHelper.startPage(Integer.parseInt(page),5);
        // 查询所有的博客列表
        List<Blog> listBlog = blogService.selectListBlog();
        List<UserBlog> userBlogs = new ArrayList<>();
        // 查询所有的用户信息
        listBlog.forEach(blog -> {
            UserBlog userBlog = new UserBlog();
            String userId = blog.getUserId();
            Admin admin = userServie.selectById(userId);
            //添加用户的用户名
            userBlog.setUsername(admin.getName());
            // 设置用户头像
            userBlog.setHeadImg(admin.getHeadImg());
            // 博客首图
            userBlog.setFlag(blog.getFlag());
            // 博客分类
            userBlog.setTypeName(typeService.selectById(blog.getTypeId()).getName());
            // 博客描述
            try {
                userBlog.setDescription(blog.getContent().substring(0,40) +"...");
            }catch (Exception e){
                throw new RuntimeException();
            }

            // 设置修改时间
            userBlog.setUpdateTime(DateUtil.format(blog.getUpdateTime(),"yyyy-MM-dd"));
            // 设置博客标题
            userBlog.setTitle(blog.getTitle());
            // 设置观看人数
            userBlog.setViews(RandomUtil.randomLong(1,99999));
            userBlog.setId(blog.getId());

            userBlogs.add(userBlog);
        });


        PageInfo<UserBlog> pageInfo = new PageInfo<>(userBlogs);
        pageInfo.setTotal(total);
        return Result.success(pageInfo);
    }

    // 查询分类最多的前五个
    @GetMapping("/types")
    public Result typeTop(){
        Long total = typeService.total();
        List<TypeTop> types = typeService.typeTop();
        types.forEach(typeTop -> {
            typeTop.setTotal(total);
        });
        return Result.success(types);
    }


    @GetMapping("/newRecommend")
    public Result getNewRecommend(){

        List<Blog> blogs = blogService.selectNewRecommend();

        List<NewRecommend> list = new ArrayList<>();


        blogs.forEach(blog -> {
            NewRecommend newRecommend = new NewRecommend();
            BeanUtils.copyProperties(blog,newRecommend);
            list.add(newRecommend);
        });
        return Result.success(list);
    }


    @GetMapping("/type/blog/{id}")
    public Result userListBlogsByTypeId(@PathVariable("id") String id){

        Long total = blogService.blogCount();
        PageHelper.startPage(1,5);
        // 查询所有的博客列表
        List<Blog> listBlog = blogService.selectByTypeId(Long.valueOf(id));
        List<UserBlog> userBlogs = new ArrayList<>();
        // 查询所有的用户信息
        listBlog.forEach(blog -> {
            UserBlog userBlog = new UserBlog();
            String userId = blog.getUserId();
            System.out.println("userId = " + blog.getUserId());
            Admin admin = userServie.selectById(userId);
            //添加用户的用户名
            userBlog.setUsername(admin.getName());
            // 设置用户头像
            userBlog.setHeadImg(admin.getHeadImg());
            // 博客首图
            userBlog.setFlag(blog.getFlag());
            // 博客分类
            userBlog.setTypeName(typeService.selectById(blog.getTypeId()).getName());
            // 博客描述
            try {
                userBlog.setDescription(blog.getContent().substring(0,40) +"...");
            }catch (Exception e){
                throw new RuntimeException();
            }

            // 设置修改时间
            userBlog.setUpdateTime(DateUtil.format(blog.getUpdateTime(),"yyyy-MM-dd"));
            // 设置博客标题
            userBlog.setTitle(blog.getTitle());
            // 设置观看人数
            userBlog.setViews(RandomUtil.randomLong(1,99999));
            userBlog.setId(blog.getId());

            userBlogs.add(userBlog);
        });


        PageInfo<UserBlog> pageInfo = new PageInfo<>(userBlogs);
        pageInfo.setTotal(total);
        return Result.success(pageInfo);
    }
}
